package first.spring.community.service;

import first.spring.community.dto.PaginationDTO;
import first.spring.community.dto.QuestionDTO;
<<<<<<< HEAD
import first.spring.community.exception.CustomizeErrorCode;
import first.spring.community.exception.CustomizeException;
import first.spring.community.mapper.QuestionExtMapper;
import first.spring.community.mapper.QuestionMapper;
import first.spring.community.mapper.UserMapper;
import first.spring.community.model.Question;
import first.spring.community.model.QuestionExample;
import first.spring.community.model.User;
import org.apache.ibatis.session.RowBounds;
=======
import first.spring.community.mapper.QuestionMapper;
import first.spring.community.mapper.UserMapper;
import first.spring.community.model.Question;
import first.spring.community.model.User;
>>>>>>> a0fc79d505bdb077dabd50bf32f5cc01c09d40f2
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionService {
    @Autowired
<<<<<<< HEAD
    private  QuestionMapper questionMapper;
    @Autowired
    private QuestionExtMapper questionExtMapper;
=======
    private QuestionMapper questionMapper;
>>>>>>> a0fc79d505bdb077dabd50bf32f5cc01c09d40f2
    @Autowired
    private UserMapper userMapper;

    public PaginationDTO list(Integer page, Integer size) {

        PaginationDTO paginationDTO = new PaginationDTO();
        Integer totalPage;
<<<<<<< HEAD
        Integer totalCount =(int)questionMapper.countByExample(new QuestionExample());
=======
        Integer totalCount =questionMapper.count();
>>>>>>> a0fc79d505bdb077dabd50bf32f5cc01c09d40f2

        if (totalCount % size == 0) {
            totalPage = totalCount / size;
        } else { totalPage = totalCount / size + 1;
        }
        if (page < 1) {
            page = 1;
        }
        if (page > totalPage) {
            page = totalPage;
        }
        paginationDTO.setPagination(totalPage,page);


        Integer offset = size * (page - 1);
<<<<<<< HEAD
        List<Question> questions= questionMapper.selectByExampleWithRowbounds(new QuestionExample(),new RowBounds(offset,size));
=======
        List<Question> questions = questionMapper.list(offset, size);
>>>>>>> a0fc79d505bdb077dabd50bf32f5cc01c09d40f2
        List<QuestionDTO> questionDTOList = new ArrayList<>();


        for (Question question : questions) {
<<<<<<< HEAD
            User user = userMapper.selectByPrimaryKey(question.getCreator());
=======
            User user = userMapper.findById(question.getCreator());
>>>>>>> a0fc79d505bdb077dabd50bf32f5cc01c09d40f2
            QuestionDTO questionDTO = new QuestionDTO();
            BeanUtils.copyProperties(question, questionDTO);
            questionDTO.setUser(user);
            questionDTOList.add(questionDTO);
        }
        paginationDTO.setQuestions(questionDTOList);
        return paginationDTO;
    }

<<<<<<< HEAD
    public PaginationDTO list(Long userId, Integer page, Integer size) {
        PaginationDTO paginationDTO = new PaginationDTO();
        Integer totalPage;

        QuestionExample questionExample=new QuestionExample();
        questionExample.createCriteria().andCreatorEqualTo(userId);
        Integer totalCount =(int)questionMapper.countByExample(questionExample);
=======
    public PaginationDTO list(Integer userId, Integer page, Integer size) {
        PaginationDTO paginationDTO = new PaginationDTO();
        Integer totalPage;

        Integer totalCount = questionMapper.countByUserId(userId);

>>>>>>> a0fc79d505bdb077dabd50bf32f5cc01c09d40f2
        if (totalCount % size == 0) {
            totalPage = totalCount / size;
        } else {
            totalPage = totalCount / size + 1;
        }

        if (page < 1) {
            page = 1;
        }
        if (page > totalPage) {
            page = totalPage;
        }

        paginationDTO.setPagination(totalPage,page);


        Integer offset = size * (page - 1);
<<<<<<< HEAD

        QuestionExample example =new QuestionExample();
        example.createCriteria().andCreatorEqualTo(userId);
        List<Question> questions = questionMapper.selectByExampleWithRowbounds(example,new RowBounds(offset,size));
=======
        List<Question> questions = questionMapper.listByUserId(userId,offset, size);
>>>>>>> a0fc79d505bdb077dabd50bf32f5cc01c09d40f2
        List<QuestionDTO> questionDTOList = new ArrayList<>();


        for (Question question : questions) {
<<<<<<< HEAD
            User user = userMapper.selectByPrimaryKey(question.getCreator());
=======
            User user = userMapper.findById(question.getCreator());
>>>>>>> a0fc79d505bdb077dabd50bf32f5cc01c09d40f2
            QuestionDTO questionDTO = new QuestionDTO();
            BeanUtils.copyProperties(question, questionDTO);
            questionDTO.setUser(user);
            questionDTOList.add(questionDTO);
        }
        paginationDTO.setQuestions(questionDTOList);
        return paginationDTO;


    }

<<<<<<< HEAD
    public QuestionDTO getById(Long id) {
        Question question = questionMapper.selectByPrimaryKey(id);
        if(question ==null){
            throw new CustomizeException(CustomizeErrorCode.QUESTION_NOT_FOUND);
        }
        QuestionDTO questionDTO = new QuestionDTO();
        BeanUtils.copyProperties(question, questionDTO);
        User user = userMapper.selectByPrimaryKey(question.getCreator());
=======
    public QuestionDTO getById(Integer id) {
        Question question = questionMapper.getById(id);
        QuestionDTO questionDTO = new QuestionDTO();
        BeanUtils.copyProperties(question, questionDTO);
        User user = userMapper.findById(question.getCreator());
>>>>>>> a0fc79d505bdb077dabd50bf32f5cc01c09d40f2
        questionDTO.setUser(user);
        return questionDTO;
    }

    public void createOrUpdate(Question question) {
<<<<<<< HEAD
        if (question.getId() == null) {
            //创建
            question.setGmtCreate(System.currentTimeMillis());
            question.setGmtModified(question.getGmtCreate());
            question.setViewCount(0);
            question.setLikeCount(0);
            question.setCommentCount(0);
            questionMapper.insert(question);
        } else {
            Question updateQuestion = new Question();
            updateQuestion.setGmtModified(System.currentTimeMillis());
            updateQuestion.setTitle(question.getTitle());
            updateQuestion.setDescription(question.getDescription());
            updateQuestion.setTag(question.getTag());
            QuestionExample example = new QuestionExample();
            example.createCriteria().andIdEqualTo(question.getId());
            int updated = questionMapper.updateByExampleSelective(updateQuestion, example);
            if (updated != 1) {
                throw new CustomizeException(CustomizeErrorCode.QUESTION_NOT_FOUND);
            }
        }
    }
    public  void incView(Long id) {
        Question question = new Question();
        question.setId(id);
        question.setViewCount(1);
        questionExtMapper.incView(question);
    }
            //更新

=======
        if(question.getId()==null){
            //创建
            question.setGmtCreate(System.currentTimeMillis());
            question.setGmtModified(question.getGmtCreate());
            questionMapper.create(question);
        }else{
            question.setGmtModified(question.getGmtCreate());
            questionMapper.update(question);
        //更新

    }
}
>>>>>>> a0fc79d505bdb077dabd50bf32f5cc01c09d40f2
}
